<template>
  <div>
    <breadcrumb :breadcrumbItem="['权限管理','角色列表']"></breadcrumb>

    <el-card>
      <el-button type="primary" @click="">添加角色</el-button>

      <el-table :data="roleList" style="width: 100%" border stripe>
        <!-- 展开列 -->
        <el-table-column type='expand'>
          <template v-slot='scope'>
            <el-row :class="['bdbottom',i1===0?'bdtop':'','vcenter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close='removeRightById(scope.row,item1.id)'>
                  {{item1.authName}}
                </el-tag>
                <i class='el-icon-caret-right'></i>               
              </el-col>
              <!-- 二级和三级权限 -->
              <el-col :span="19">
                <el-row :class="[i2===0?'':'bdtop','vcenter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                  <!-- 二级权限 -->
                  <el-col :span="6">
                    <el-tag type='success' closable @close='removeRightById(scope.row,item2.id)'>
                      {{item2.authName}}
                    </el-tag>
                    <i class='el-icon-caret-right'></i> 
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18">
                    <el-tag v-for='(item3,i3) in item2.children' :key="item2.id" type='warning'
                      closable @close='removeRightById(scope.row,item3.id)'>
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>

        <el-table-column type='index' label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>

        <el-table-column label="操作" width="350px">
          <template v-slot="scope">
            <!-- 编辑按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="">删除</el-button>
            <!-- 分配权限 -->
            <el-button type="warning" icon="el-icon-setting" size="mini">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
  import Breadcrumb from 'components/common/Breadcrumb.vue'
  export default {
    components: {
      Breadcrumb
    },
    data() {
      return {
        roleList: []
      }
    },
    created() {
      this.getRoleList()
    },
    methods: {
      async getRoleList() {
        const { data: res } = await this.$http.get('roles')
        if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
        console.log(res)
        this.roleList = res.data
      },
      removeRightById(role,rightId){
        this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res}=await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
          if(res.meta.status!==200) return this.$message.error("删除权限失败")
          this.$message.success('删除成功!');
          role.children=res.data
        }).catch(() => {
          this.$message.info('已取消删除');
        });

      }
    },
  }
</script>

<style scoped>
.el-tag{
  margin:7px
}

.bdtop{
  border-top: 1px solid #eee;
}

.bdbottom{
  border-bottom: 1px solid #eee;
}

.vcenter{
  display:flex;
  align-items: center;
}
</style>